<?php
//127.0.0.1:2222/db.arpggame.com/auto/item_0.php?class=2&sub=0&page=1&max=10
header("Content-type: text/html; charset=utf-8");

require_once 'Snoopy-1.2.4/Snoopy.class.php';
require_once "mysql_connect.php"; //connect

//require_once '../include/water.php';//水印
function preg_substr($start, $end, $str) // 正则截取函数     
{     
    $temp = preg_split($start, $str);     
    $content = preg_split($end, $temp[1]);     
    return $content[0];     
}  
function str_substr($start, $end, $str) // 字符串截取函数     
{     
    $temp = explode($start, $str, 2);     
    $content = explode($end, $temp[1], 2);     
    return $content[0];     
} 
function strtr_words($str)//同义词替换
{
    $words=array();
    $content = file_get_contents('same.txt');//词库
    $content = str_replace( "\r", "",$content); //去掉换行符(以便兼容Linux主机)
    $content = preg_split('/\n/', $content, -1, PREG_SPLIT_NO_EMPTY);//\n分割字符
        foreach($content as $k=>$v)
        {
            if($k!=0)
            {
                $str_data = explode('=',$v);//关键词分割符
                $words+=array("$str_data[0]"=>"$str_data[1]");
            }
        }
    return strtr($str,$words);//返回结果
}

$id = $_GET['id'] ? $_GET['id'] : "11";

$n_url = $_GET['url'] ? $_GET['url'] : "http://www.battlenet.com.cn/wow/zh/tool/talent-calculator/$id";

$classid = $_GET['id'] ? $_GET['classid'] : "17";

$max = $_GET['max'] ? $_GET['max'] : '10';


set_time_limit(0);


//for ($i = $min; $i <= $max; $i++) {

	
	$url = $n_url;      

	$snoopy = new Snoopy;   
	$snoopy->fetch($url); //获取所有内容    

	$snoopy->cookies["PHPSESSID"] = 'fc106b1918bd522cc863f36890e6fff7'; //伪装sessionid   
	$snoopy->agent = "(compatible; MSIE 4.01; MSN 2.5; AOL 4.0; Windows 98)"; //伪装浏览器   
	$snoopy->referer = "http://www.google.com"; //伪装来源页地址 http_referer   
	$snoopy->rawheaders["Pragma"] = "no-cache"; //cache 的http头信息   
	$snoopy->rawheaders["X_FORWARDED_FOR"] = "127.0.0.1"; //伪装ip   
	//$snoopy->submit($action,$formvars);   
	$snoopy->expandlinks = true; //是否补全链接

	$results= $snoopy->results;//输出全文
	//$results = iconv("gb2312","UTF-8",$results);
	//echo $snoopy->fetchtext; //获取文本内容（去掉html代码）   
	//echo $snoopy->fetchlinks; //获取链接   
	//echo $snoopy->fetchform ; //获取表单 


	//$results = str_substr('<table cellspacing="0" cellpadding="0" summary="forum_7">', '</table>', $results);

	$results = str_substr('<div class="spells">', '<script type="text/javascript">', $results);


	$results = ereg_replace("style=\"display: none\"","",$results);

	
	$results = explode("<span class=\"item-link\">", $results);

	
	//preg_match_all("/<div class=\"tc-tooltip\">\[(.*)\]<td class=\"col-level\">/",$results,$page);


	
foreach ($results as $k=>$outpage) {
	if ($k == 0){
		//
	}else{



	$images = str_substr('<div class="tc-tooltip">', '</span>', $outpage);
	$icon = str_substr('url("', '")', $images);
	

	//preg_match_all('/<div class="tc-tooltip">(.+?)<td class="col-level">/',$results,$page);
	
	$name = str_substr('<h3>', '</h3>', $outpage);

	
	$specs = str_substr('</h3>', '</td>', $outpage);
	$specs = "<div><div>".$specs;

	
					
					//SAVE ICON
					$icon_url 	 = trim(str_ireplace("http://content.battlenet.com.cn/", '', $icon));
					$now_name 	 = "/".basename($icon);
					$now_dir	 = trim(str_ireplace($now_name, '', $icon_url));

					$dir_name 	 = "../uploads/".$now_dir; 
					if(!file_exists($dir_name)){
						mkdir($dir_name, 0777, true);
						@chmod($dir_name, 511);//0777 八进制
					}
					
					$icon_url= $now_dir."/".(basename($icon));
					/*
					if(!file_exists($dir_name."/".(basename($icon)))){
						file_put_contents($dir_name."/".(basename($icon)), file_get_contents($icon));
					}*/

	
			
			if ($name == "" ){
				
				echo "标题为空";
			}else{
				//入库
				
				mysql_query('set names utf8');  // select u8
				$cf_result=mysql_query("select * from db_wow_spell WHERE name='$name' AND classid = '$classid'");//重复性
				if(mysql_num_rows($cf_result)){
					echo $name.'——重复文章跳过。。。<br/>';	
				}else {
				
					if ($specs==""){
						echo "内容为空";
					}else{

						$sql = "INSERT INTO  db_wow_spell (name, icon, specs, classid)
						VALUES ('$name','$icon_url','$specs','$classid')";// insert
						mysql_query('set names utf8');  // select u8
						
						if(mysql_query($sql)){
							
							echo ($name.'——已成功发布'."<br/>"); //if mysql error

						}else{
							echo '连接出错POST表，未发布。跳过'.mysql_error()."<br/>";
						}
					
						}
					}
					

			}
			
		}

}
//}



//if ($i > ($max + 0.5)){
	echo "<br/><font color:'red'>全部完成！$i</font>";
	echo "<script>alert('全部完成！')</script>";
//}else{
//	echo "<script>location.href='item_0.php?page=$i&max=$max&class=$class&sub=$sub&inv=$inv'</script>";
//}
?>